package com.css.apps.base.user.action;

import com.css.apps.base.role.action.RoleItem;
import com.css.apps.base.role.model.SRole;
import com.css.apps.base.user.model.SUser;
import com.css.core.configuration.Environment;
import com.css.db.page.Page;
import com.css.db.query.QueryCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("serial")
public class DirUserRole extends AdminAction {
	
	private static Log log = LogFactory.getLog(DirUserRole.class);
	private Page page;
	private String userId;
	private List<SRole> RoleList;
	/*private List<Object[]> postRoleList = new ArrayList<>();*/
	private boolean Flag = true;

	public DirUserRole() {
		page = new Page();
		page.setCurrentPage(1);
		page.setPageSize(11);
		page.setCountField("a.uuid");
	}

	@SuppressWarnings("unchecked")
	@Override
	protected String adminGo() {
		try {
			SUser sUser = QueryCache.get(SUser.class, userId);
			if (sUser == null) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.failed"));
				return Action.ERROR;
			}
			List<String> userRoleList = sUser.getRoleList().getListById();// 用户-角色关联
			// List<String> userPostList = sUser.getPostList().getListById();// 用户-岗位关联
			// List<String> postroleList = new ArrayList<String>(); // 所有用户关联的岗位关联的角色（除去用户已管理的角色）
			// List<String> postroleIdList = new ArrayList<String>(); // 用户关联的角色+岗位关联的角色，即所有角色
			List<String> roleIdList = new ArrayList<String>(); // 用户关联的角色列表
			for (String roleid : userRoleList) {
				roleIdList.add(roleid.trim());
			}
			// postroleIdList.addAll(roleIdList);
			// for (String postId : userPostList) {
			// 	// 每个岗位所关联的角色，去掉用户直接关联的角色,去掉岗位之间重复关联的角色
			// 	List<SRole> postrolelist = SPostItem.getRoleList(postId);
			// 	if (postrolelist != null) {
			// 		for (SRole roleitem : postrolelist) {
			// 			if (!postroleIdList.contains(roleitem.getUuid())) {
			// 				postroleList.add(roleitem.getUuid());
			// 				postroleIdList.add(roleitem.getUuid());
			// 			}
			// 		}
			// 	}
			// }
			// for (String roleId : postroleList) {
			// 	SRole role = QueryCache.get(SRole.class, roleId);
			// 	String postNames = "";
			// 	for (String postId : userPostList) {
			// 		SPost post = QueryCache.get(SPost.class, postId);
			// 		List<String> postRolseIdList = SPostItem.getRoleIdList(postId);
			// 		if (postRolseIdList != null && postRolseIdList.contains(roleId)) { // 如果此岗位关联当前角色，把岗位的名称加入
			// 			postNames += post.getName();
			// 			postNames += ",";
			// 		}
			// 	}
			// 	Object[] obj = new Object[4];
			// 	obj[0] = roleId;
			// 	obj[1] = role.getName();
			// 	obj[2] = postNames.substring(0, postNames.length() - 1);
			// 	obj[3] = role.getOpenFlag();
			// 	postRoleList.add(obj);
			// }

			if (roleIdList.size() == RoleItem.getSRoleCount(sUser.getUserType())) {
				Flag = false;
			}
			RoleList = QueryCache.idToObj(SRole.class, roleIdList);
			return SUCCESS;
		} catch (Exception ex) {
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			log.error(ex.getMessage(), ex);
			return Action.ERROR;
		}
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public List<SRole> getRoleList() {
		return RoleList;
	}

	public void setRoleList(List<SRole> roleList) {
		RoleList = roleList;
	}

	public boolean isFlag() {
		return Flag;
	}

	public void setFlag(boolean flag) {
		Flag = flag;
	}

}
